home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / resize2a / resizer.ctl < prev    next >
Text File  |  1998-10-14  |  9KB  |  339 lines

  1. VERSION 5.00
  2. Begin VB.UserControl Resizer 
  3.    ClientHeight    =   480
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   495
  7.    InvisibleAtRuntime=   -1  'True
  8.    ScaleHeight     =   480
  9.    ScaleWidth      =   495
  10.    ToolboxBitmap   =   "Resizer.ctx":0000
  11.    Begin VB.Image Image1 
  12.       Height          =   480
  13.       Left            =   0
  14.       Picture         =   "Resizer.ctx":0182
  15.       Top             =   0
  16.       Width           =   480
  17.    End
  18. End
  19. Attribute VB_Name = "Resizer"
  20. Attribute VB_GlobalNameSpace = False
  21. Attribute VB_Creatable = True
  22. Attribute VB_PredeclaredId = False
  23. Attribute VB_Exposed = True
  24. Option Explicit
  25.  
  26. ' if True, also fonts are resized '
  27. Public ResizeFont As Boolean
  28.  
  29. ' if True, form's height/width ratio is preserved '
  30. Public KeepRatio As Boolean
  31.  
  32. Private Type TControlInfo
  33.        
  34.        ctrl As Control
  35.        Left As Single
  36.        Top As Single
  37.        Width As Single
  38.        Height As Single
  39.        FontSize As Single
  40.        
  41. End Type
  42.  
  43. Private Type TAllowChanges
  44.   
  45.        AllowChangeTop As Boolean
  46.        AllowChangeLeft As Boolean
  47.        AllowChangeWidth As Boolean
  48.        AllowChangeHeight As Boolean
  49.         
  50. End Type
  51.  
  52. ' this array holds the original position  '
  53. ' and size of all controls on parent form '
  54. Dim Controls() As TControlInfo
  55.  
  56. ' a reference to the parent form '
  57. Private WithEvents ParentForm As Form
  58. Attribute ParentForm.VB_VarHelpID = -1
  59.  
  60. ' parent form's size at load time '
  61. Private ParentWidth As Single
  62. Private ParentHeight As Single
  63.  
  64. ' ratio of original height/width '
  65. Private HeightWidthRatio As Single
  66.  
  67. Private Function CheckForChanges(ByVal TagToUse As String) As TAllowChanges
  68.   
  69.   Dim ChangesToAllow As TAllowChanges
  70.   
  71.   ChangesToAllow.AllowChangeTop = True
  72.   ChangesToAllow.AllowChangeLeft = True
  73.   ChangesToAllow.AllowChangeWidth = True
  74.   ChangesToAllow.AllowChangeHeight = True
  75.     
  76.   If TagToUse <> "" Then
  77.     
  78.     If UCase(Left(TagToUse, 9)) = "MSIRESIZE" Then
  79.       
  80.       ChangesToAllow.AllowChangeTop = False
  81.       ChangesToAllow.AllowChangeLeft = False
  82.       ChangesToAllow.AllowChangeWidth = False
  83.       ChangesToAllow.AllowChangeHeight = False
  84.     
  85.       If Mid(TagToUse, 10, 1) = "Y" Then
  86.       
  87.         ChangesToAllow.AllowChangeLeft = True
  88.         
  89.       End If
  90.       
  91.       If Mid(TagToUse, 11, 1) = "Y" Then
  92.       
  93.         ChangesToAllow.AllowChangeTop = True
  94.         
  95.       End If
  96.       
  97.       If Mid(TagToUse, 12, 1) = "Y" Then
  98.       
  99.         ChangesToAllow.AllowChangeWidth = True
  100.         
  101.       End If
  102.       
  103.       If Mid(TagToUse, 13, 1) = "Y" Then
  104.       
  105.         ChangesToAllow.AllowChangeHeight = True
  106.         
  107.       End If
  108.       
  109.     End If
  110.     
  111.   End If
  112.   
  113.   CheckForChanges = ChangesToAllow
  114.   
  115. End Function
  116.  
  117. Private Sub ParentForm_Load()
  118.  
  119.   ' the ParentWidth variable works as a flag '
  120.   ParentWidth = 0
  121.   
  122.   ' save original ratio '
  123.   HeightWidthRatio = ParentForm.Height / ParentForm.Width
  124.   
  125. End Sub
  126.  
  127. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  128.   
  129.   ResizeFont = PropBag.ReadProperty("ResizeFont", False)
  130.   KeepRatio = PropBag.ReadProperty("KeepRatio", False)
  131.   
  132.   If Ambient.UserMode = False Then
  133.     
  134.     Exit Sub
  135.   
  136.   End If
  137.   
  138.   ' store a reference to the parent form and start receiving events '
  139.   Set ParentForm = Parent
  140.   
  141. End Sub
  142. Private Sub UserControl_Resize()
  143.   
  144.   ' refuse to resize '
  145.   Image1.Move 0, 0
  146.   UserControl.Width = Image1.Width
  147.   UserControl.Height = Image1.Height
  148.   
  149. End Sub
  150.  
  151. ''''''''''''''''''''''''''''''''''''''''''''
  152. ' trap the parent form's Resize event      '
  153. ' this include the very first resize event '
  154. ' that occurs soon after form's load       '
  155. ''''''''''''''''''''''''''''''''''''''''''''
  156. Private Sub ParentForm_Resize()
  157.   
  158.   If ParentWidth = 0 Then
  159.     
  160.     Rebuild
  161.   
  162.   Else
  163.     
  164.     Refresh
  165.   
  166.   End If
  167.   
  168. End Sub
  169.  
  170. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  171. ' save size and position of all controls on parent form                  '
  172. ' you should manually invoke this method each time you add a new control '
  173. ' to the form (through Load method of a control array)                   '
  174. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  175. Sub Rebuild()
  176.   
  177.   ' rebuild the internal table
  178.   Dim i As Integer
  179.   Dim ctrl As Control
  180.   
  181. '  Dim Changes As TAllowChanges
  182.   
  183.   ' this is necessary for controls that don't support
  184.   ' all properties (e.g. Timer controls)
  185.   On Error Resume Next
  186.     
  187.   If Ambient.UserMode = False Then
  188.     
  189.     Exit Sub
  190.     
  191.   End If
  192.     
  193.   ' save a reference to the parent form, and its initial size
  194.   Set ParentForm = UserControl.Parent
  195.   ParentWidth = ParentForm.ScaleWidth
  196.   ParentHeight = ParentForm.ScaleHeight
  197.     
  198.   ' read the position of all controls on the parent form
  199.   ReDim Controls(ParentForm.Controls.Count - 1) As TControlInfo
  200.     
  201.   For i = 0 To ParentForm.Controls.Count - 1
  202.      
  203.      Set ctrl = ParentForm.Controls(i)
  204.         
  205. '     Changes = CheckForChanges(ctrl)
  206.      
  207.      With Controls(i)
  208.                  Set .ctrl = ctrl
  209.                      
  210. '                     If Changes.AllowChangeLeft = True Then
  211.                        .Left = ctrl.Left
  212. '                     End If
  213. '                     If Changes.AllowChangeTop = True Then
  214.                        .Top = ctrl.Top
  215. '                     End If
  216. '                     If Changes.AllowChangeTop = True Then
  217.                        .Width = ctrl.Width
  218. '                     End If
  219. '                     If Changes.AllowChangeTop = True Then
  220.                        .Height = ctrl.Height
  221. '                     End If
  222.                      .FontSize = ctrl.Font.Size
  223.      End With
  224.         
  225.   Next
  226.   
  227. End Sub
  228.  
  229. '''''''''''''''''''''''''''''''''''''''''''''''''''''''
  230. ' update size and position of controls on parent form '
  231. '''''''''''''''''''''''''''''''''''''''''''''''''''''''
  232. Sub Refresh()
  233.   
  234.   Dim i As Integer
  235.   Dim ctrl As Control
  236.   Dim minFactor As Single
  237.   Dim widthFactor As Single
  238.   Dim heightFactor As Single
  239.   
  240.   Dim Changes As TAllowChanges
  241.     
  242.   ' inhibits recursive calls if KeepRatio = True '
  243.   Static executing As Boolean
  244.   
  245.   If executing Then
  246.     
  247.     Exit Sub
  248.     
  249.   End If
  250.     
  251.   If Ambient.UserMode = False Then
  252.     
  253.     Exit Sub
  254.     
  255.   End If
  256.     
  257.   If KeepRatio Then
  258.     
  259.     executing = True
  260.     
  261.     ' we must keep original ratio '
  262.     ParentForm.Height = HeightWidthRatio * ParentForm.Width
  263.     executing = False
  264.   
  265.   End If
  266.     
  267.   ' this is necessary for controls that don't support '
  268.   ' all properties (e.g. Timer controls)              '
  269.   On Error Resume Next
  270.  
  271.   widthFactor = ParentForm.ScaleWidth / ParentWidth
  272.   heightFactor = ParentForm.ScaleHeight / ParentHeight
  273.   
  274.   ' take the lesser of the two '
  275.   If widthFactor < heightFactor Then
  276.     
  277.     minFactor = widthFactor
  278.   
  279.   Else
  280.     
  281.     minFactor = heightFactor
  282.   
  283.   End If
  284.     
  285.   ' this is a regular resize '
  286.   For i = 0 To UBound(Controls)
  287.         
  288.      Changes = CheckForChanges(Controls(i).ctrl.Tag)
  289.      
  290.      With Controls(i)
  291.             
  292.                      ' the change of font must occur *before* the resizing '
  293.                      ' to account for companion scrollbar of listbox '
  294.                      ' and other similar controls '
  295.                      If ResizeFont Then
  296.                        
  297.                        .ctrl.Font.Size = .FontSize * minFactor
  298.                      
  299.                      End If
  300.                      
  301.                      ' move and resize the controls - we can't use a Move '
  302.                      ' method because some controls do not support the change '
  303.                      ' of all the four properties (e.g. Height with comboboxes) '
  304.                      If Changes.AllowChangeLeft = True Then
  305.                        
  306.                        .ctrl.Left = .Left * widthFactor
  307.                      
  308.                      End If
  309.                      
  310.                      If Changes.AllowChangeTop = True Then
  311.                        
  312.